using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace SDS.SharePoint.CustomConditions.Features.ApprovalStatus
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("85b9b8cb-4646-4bb3-94f8-7e8954d74367")]
    public class ApprovalStatusEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature != null &&
                properties.Feature.Parent is SPWebApplication)
                this.UpdateWebConfig(properties.Feature.Parent as SPWebApplication, false);
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature != null &&
                properties.Feature.Parent is SPWebApplication)
                this.UpdateWebConfig(properties.Feature.Parent as SPWebApplication, true);
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}


        private void UpdateWebConfig(SPWebApplication tWebApp, bool remove)
        {
            SPWebConfigModification authorizedTypeConfigNode = this.GetAuthorizedTypeConfigNode(System.Reflection.Assembly.GetCallingAssembly().FullName, "SDS.SharePoint.CustomConditions");
            if (authorizedTypeConfigNode != null)
            {
                if (remove)
                    tWebApp.WebConfigModifications.Remove(authorizedTypeConfigNode);
                else
                    tWebApp.WebConfigModifications.Add(authorizedTypeConfigNode);
                tWebApp.Update(true);
                tWebApp.WebService.ApplyWebConfigModifications();
            }
        }

        private SPWebConfigModification GetAuthorizedTypeConfigNode(string assembly, string assemblyNamespace)
        {
            return new SPWebConfigModification()
            {
                Type = (SPWebConfigModification.SPWebConfigModificationType)0,
                Name = String.Format("authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']", (object)assembly, (object)assemblyNamespace),
                Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes",
                Owner = assemblyNamespace,
                Sequence = 0U,
                Value = String.Format("<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True' />", (object)assembly, (object)assemblyNamespace)
            };
        }
    
    } // class
} // namespace
